package com.lp.mgt.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lp.mgt.commons.CustomResult;
import com.lp.mgt.entities.generate_dto.BeanField;
import com.lp.mgt.entities.generate_dto.GenerateDetail;
import com.lp.mgt.entities.generate_dto.GenerateInput;
import com.lp.mgt.entities.generate_dto.TableInfo;
import com.lp.mgt.services.GenerateService;



/**
 * 代码生成接口
 * 
 * @author 小威老师 xiaoweijiagou@163.com
 *
 */
//@Api(tags = "代码生成")
@RestController
@RequestMapping("/generate")
public class GenerateCtr {

	@Autowired
	private GenerateService generateService;

	//@ApiOperation("根据表名显示表信息")
	@GetMapping(params = { "tableName" })
	//@RequiresPermissions("generate:edit")
	public GenerateDetail generateByTableName(String tableName) {
		GenerateDetail detail = new GenerateDetail();
		detail.setBeanName(generateService.upperFirstChar(tableName));
		List<BeanField> fields = generateService.listBeanField(tableName);
		detail.setFields(fields);

		return detail;
	}

	//@LogAnnotation
	//@ApiOperation("生成代码")
	@PostMapping
	//@RequiresPermissions("generate:edit")
	public void save(GenerateInput input) {
		generateService.saveCode(input);
	}

	
	@GetMapping("/generatePage")
	public ModelAndView toCreateCodePage() {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/generate/generatePage.html");
		return mv;
	}
	
	@GetMapping("/dbTables")
	public ModelAndView toTableListPage() {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/generate/db_tableList.html");
		return mv;
	}
	
	
	@RequestMapping("/listDBTables")
	public CustomResult getDbTableList(TableInfo tableinfo) {
		Map<String,Object> map=new HashMap<>();
		map=generateService.getDbTableList(tableinfo);
		return new CustomResult().success(true, map);
	}
}
